#include <QMessageBox>
#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "loginwidget.h"
#include "housecreatewidget.h"
#include "myconfig.h"

MainWidget::MainWidget(TcpSession *session, LoginWidget* loginWidget, const QString &username, const QString &nickname, QWidget *parent):
    QWidget(parent), ui(new Ui::MainWidget),loginWidget(loginWidget),username(username),nickname(nickname),session(session)
{
    ui->setupUi(this);
    //本界面
    this->setWindowTitle("直播间列表");
    ui->label->setText("尊敬的 "+nickname+"("+username+") , 欢迎登录直播间");

    //表格初始化
    QStringList headers;
    headers<<"ID"<<"直播间"<<"播主"<<"人数(当前/上限)"<<"开始时间"<<"结束时间"<<"预留";
    ui->tableWidget->setColumnCount(headers.size());
    ui->tableWidget->setHorizontalHeaderLabels(headers);

    //master 子界面（hide-show的跳转方式）
    masterWidget=new LiveMasterWidget(session,this, parent);
    masterWidget->setWindowTitle("直播间-主播界面");
    masterWidget->hide();
    slaveWidget=new LiveSlaveWidget(session,this, parent);
    slaveWidget->setWindowTitle("直播间-用户界面");
    slaveWidget->hide();
    houseCreateWidget = new HouseCreateWidget(session,this, parent);
    houseCreateWidget->setWindowTitle("创建房间");
    houseCreateWidget->hide();

    //网络初始化
    QObject::connect(session, &TcpSession::houselist_ret_signal, this, &MainWidget::on_houselist_ret_signal);
    QObject::connect(session, &TcpSession::logout_call_signal, this, &MainWidget::on_logout_call_signal);
    QObject::connect(session, &TcpSession::house_enter_signal, this, &MainWidget::on_house_enter_signal);
}

void MainWidget::resizeEvent(QResizeEvent *event)
{
    int cols=ui->tableWidget->columnCount();
    int colWidth=this->size().width()/cols;
    for(int i=0;i<cols;i++)
        ui->tableWidget->setColumnWidth(i, colWidth);
}

void MainWidget::show()
{
    //查询直播间列表
    Pack pack;
    pack.setType(HOUSE_LIST);
    pack.setFrame(CALL);
    pack.append(username);
    session->send((char*)&pack, pack.getPacksize());
    //刷新界面
    ui->label->setText("尊敬的 "+nickname+"("+username+") , 欢迎登录直播间");
    this->QWidget::show();
}

void MainWidget::show(TcpSession *session, LoginWidget *loginWidget, const QString &username, const QString &nickname)
{
    this->session=session;
    this->loginWidget=loginWidget;
    this->username=username;
    this->nickname=nickname;
    this->show();
}

void MainWidget::on_tableWidget_cellDoubleClicked(int row, int column)
{
    QString owner=ui->tableWidget->item(row, 2)->text();
    QString housename=ui->tableWidget->item(row, 1)->text();
    int houseID=ui->tableWidget->item(row,0)->text().toInt();
    if(owner == nickname){
        QMessageBox::information(this, "身份", "这是您创建的直播间，将以播主的身份进入");
        this->hide();
        masterWidget->show(username, nickname, housename, houseID);
        return;
    }
    QMessageBox::information(this, "身份", "您将以【看客】的身份进入");
    this->hide();
    slaveWidget->show(username, nickname, housename, houseID);
    return;
}

void MainWidget::on_pushButton_logout_clicked()
{
    Pack pack(LOGOUT, CALL);
    pack.append(username);
    session->send((char*)&pack, pack.getPacksize());
    //
    this->hide();
    loginWidget->show();
}

void MainWidget::on_pushButton_createHouse_clicked()
{
    this->hide();
    houseCreateWidget->show(username, nickname);
}

void MainWidget::on_houselist_ret_signal(const QStringList &fields)
{
    //刷新表格
    int rowCnt = ui->tableWidget->rowCount();
    for(int i=rowCnt-1;i>=0;i--){
        ui->tableWidget->removeRow(i);
    }
    //写入新的数据
    int row=0;
    int col=0;
    for(int i=0;i<fields.size();i++){
        if(col == 0)
            ui->tableWidget->insertRow(row);
        if(col != 7){//最后一列不显示
            QTableWidgetItem* item=new QTableWidgetItem(fields[i]);
            item->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(row, col, item);
        }
        //
        col++;
        if(col==7){
            row++;
            col=0;
        }
    }
    if(row == 0){
        QMessageBox::information(this, "查询结果", "直播间列表目前为空");
    }
}

void MainWidget::on_logout_call_signal(const QString & msg)
{
    QMessageBox::critical(this,"登出提示", msg);
    exit(EXIT_SUCCESS);
}

void MainWidget::on_house_enter_signal(bool res, const QString &msg)
{
    if(res==false){
        QMessageBox::critical(this, "进入房间失败", msg);
        return;
    }
    //TODO进入房间成功
    QMessageBox::information(this, "进入房间成功", "进入房间成功");
}
